| Conditions | 1 |
| Paths | 1 |
| Total Lines | 108 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 16 | function (state, data, visibility, util, format, reactionService) { |
||
| 17 | let ct = this; |
||
| 18 | ct.state = state; |
||
| 19 | ct.data = data; |
||
| 20 | ct.util = util; |
||
| 21 | ct.format = format; |
||
| 22 | |||
| 23 | function update(player) { |
||
| 24 | for(let slot of player.element_slots){ |
||
| 25 | for (let reaction of slot.reactions) { |
||
| 26 | if (!reaction.active) { |
||
| 27 | continue; |
||
| 28 | } |
||
| 29 | reactionService.react(numberToReact(player, reaction.reaction), reaction.reaction, player); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | function numberToReact(player, reaction) { |
||
| 35 | let power = ct.reactionPower(player); |
||
| 36 | let number = power; |
||
| 37 | for(let resource in reaction.reactants){ |
||
| 38 | number = Math.min(number, player.resources[resource].number); |
||
| 39 | } |
||
| 40 | return number; |
||
| 41 | } |
||
| 42 | |||
| 43 | /* Calculates the reaction power based on the reaction upgrades */ |
||
| 44 | ct.reactionPower = function(player) { |
||
| 45 | let level = player.global_upgrades.reaction_bandwidth; |
||
| 46 | let upgrade = data.global_upgrades.reaction_bandwidth; |
||
| 47 | let basePower = upgrade.power; |
||
| 48 | let polynomial = upgrade.power_poly; |
||
| 49 | return basePower * Math.floor(Math.pow(level, polynomial)); |
||
| 50 | }; |
||
| 51 | |||
| 52 | /* Calculates the number of reaction slots based on the reaction upgrades */ |
||
| 53 | ct.reactionSlots = function (player) { |
||
| 54 | let level = player.global_upgrades.reaction_slots; |
||
| 55 | let upgrade = data.global_upgrades.reaction_slots; |
||
| 56 | let basePower = upgrade.power; |
||
| 57 | let multiplier = upgrade.power_mult; |
||
| 58 | return basePower * Math.floor(multiplier * level); |
||
| 59 | }; |
||
| 60 | |||
| 61 | ct.reactionSize = function (player) { |
||
| 62 | let size = 0; |
||
| 63 | for(let slot of player.element_slots){ |
||
| 64 | size += slot.reactions.length; |
||
| 65 | } |
||
| 66 | return size; |
||
| 67 | }; |
||
| 68 | |||
| 69 | /* Adds a new reaction to the player list */ |
||
| 70 | ct.addReaction = function (player, slot, key) { |
||
| 71 | if(ct.reactionSize(player) >= ct.reactionSlots(player)){ |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | let reaction = data.reactions[key]; |
||
| 75 | slot.reactions.push({ |
||
| 76 | active: false, |
||
| 77 | reaction: angular.copy(reaction) |
||
| 78 | }); |
||
| 79 | }; |
||
| 80 | |||
| 81 | ct.removeReaction = function (slot, item) { |
||
| 82 | for(let i = 0; i < slot.reactions.length; i++){ |
||
| 83 | if(slot.reactions[i] === item){ |
||
| 84 | slot.reactions.splice(i, 1); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | }; |
||
| 88 | |||
| 89 | ct.visibleReactions = function(slot) { |
||
| 90 | return slot.reactions; |
||
| 91 | }; |
||
| 92 | |||
| 93 | ct.availableReactions = function(slot) { |
||
| 94 | return visibility.visible(data.reactions, isReactionAvailable, slot.element); |
||
| 95 | }; |
||
| 96 | |||
| 97 | function isReactionAvailable(entry, currentElement) { |
||
| 98 | let available = true; |
||
| 99 | let reaction = data.reactions[entry]; |
||
| 100 | for(let resource in reaction.reactant){ |
||
| 101 | available = available && state.player.resources[resource].unlocked; |
||
| 102 | } |
||
| 103 | // Workaround to reuse the visibility function. It expects an object with the |
||
| 104 | // reaction inside |
||
| 105 | let reactionObject = {reaction:reaction}; |
||
| 106 | return available && isReactionVisible(reactionObject, currentElement); |
||
| 107 | } |
||
| 108 | |||
| 109 | function isReactionVisible(entry, currentElement) { |
||
| 110 | let reaction = entry.reaction; |
||
| 111 | if(reaction.elements.length === 0){ |
||
| 112 | return true; |
||
| 113 | } |
||
| 114 | for(let element of reaction.elements){ |
||
| 115 | if(element === currentElement){ |
||
| 116 | return true; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | state.registerUpdate('reactions', update); |
||
| 123 | }]); |
||
| 124 |